package day3;

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

//用户管理系统 序列化实战功能
//1.用户查询
//2.用户添加
//3.用户删除
//4.用户修改
//5.退出系统
//并且退出系统后，再次打开系统，数据依然存在(序列化)(数据库)
public
class UserSystem {
    //创建一个集合，用于存储用户信息
    static List<User> userList = new ArrayList<>();
    static Scanner sc = new Scanner(System.in);
    static {//静态代码块，在类加载时执行
        //刚刚打开系统，就需要读取文件中的数据 做反序列化
        input();
    }
    public static
    void main(String[] args) {
        menu();
    }

    public static
    void menu() {
        while (true) {
            System.out.println("用户管理系统");
            System.out.println("1.用户查询");
            System.out.println("2.用户添加");
            System.out.println("3.用户删除");
            System.out.println("4.用户修改");
            System.out.println("5.退出系统");
            System.out.println("请输入操作");
            int choice = sc.nextInt();
            sc.nextLine();
            switch (choice) {
                case 1: show();break;
                case 2: addUser(); break;
                case 3: break;
                case 4: break;
                case 5:
                    System.out.println("退出系统成功");return;
                default:
                    System.out.println("输入错误");
            }
        }
    }
    public static void show(){
        System.out.println("编号\t用户名\t密码\t城市");
        for(int i=0;i<userList.size();i++){
            User u=userList.get(i);//获取集合中的每一个元素
            System.out.println((i+1)+"\t"+u.getName()+"\t"+u.getPassword()+"\t"+u.getCity());
        }
    }

    public static
    void addUser() {
        System.out.println("请输入用户名");
        String name = sc.nextLine();
        System.out.println("请输入密码");
        String password = sc.nextLine();
        System.out.println("请输入城市");
        String city = sc.nextLine();
        userList.add(new User(name, password, city));
        System.out.println("添加成功");
        //数据修改了 就需要重新序列化
        output();
    }
    public static void deleteUser(){
        output();
    }
    public static void updateUser(){
        output();
    }
    public static void output(){
        try (
                FileOutputStream fos=new FileOutputStream("D:\\IO\\user.txt");
                ObjectOutputStream oos=new ObjectOutputStream(fos);
                ){
            oos.writeObject(userList);
            System.out.println("集合更新成功");
        }catch (IOException e){
            e.printStackTrace();
        }
    }
    public static void input(){
        try (
                FileInputStream fis=new FileInputStream("D:\\IO\\user.txt");
                ObjectInputStream ois=new ObjectInputStream(fis);
                ){
            userList= (List<User>) ois.readObject();
            System.out.println("集合读取成功");
        }catch (FileNotFoundException e){
            System.out.println("欢迎第一使用，系统自动创建文件");
        }catch (IOException | ClassNotFoundException e){
            e.printStackTrace();
        }
    }
}
class User implements Serializable {
    private String name;
    private String password;
    private String city;

    public
    String getName() {
        return name;
    }

    public
    void setName(String name) {
        this.name = name;
    }

    public
    String getPassword() {
        return password;
    }

    public
    void setPassword(String password) {
        this.password = password;
    }

    public
    String getCity() {
        return city;
    }

    public
    void setCity(String city) {
        this.city = city;
    }

    public
    User(String name, String password, String city) {
        this.name = name;
        this.password = password;
        this.city = city;
    }
}